/* keyboard.h
 * By: Spencer Clark
 * Date created: March 18th 2011
 * Date modified: March 21st 2011
 * Modified by: Spencer Clark
 * Note: This file is essentially a modified version of the kdb.h file found in
 *       xv6 OS, source can be found at:
 *       pdos.csail.mit.edu/6.828/xv6/xv6-rev5.pdf
 */

#ifndef _XXXOS_KEYBOARD_H
#define _XXXOS_KEYBOARD_H

#include <interrupt.h>
#include <stdint.h>

#define KBD_STAT_P 0x64 /* keyboard controller status port */
#define KBD_IN_BUF 0x01 /* keyboard data in buffer */
#define KBD_DATA_P 0x60 /* keyboard data port */


#define NOK 0     /* no/invalid key */
#define ESC 0x1B  /* escape key ASCII code */
#define NWL 0xA   /* newline ASCII */
#define BSP 0x8   /* backspace ASCII */
#define TAB 0x9   /* tab ASCII */
#define BSL 0x5C  /* backslash ASCII */
#define QUO 0x27  /* single quote ASCII */

#define SHIFT (1<<0) /* shift modifier */

/* Use keyboard_getc to grab the character related to the key being pressed and
 * print it to the screen.
 */
void keyboard_int(void);

/* Set up the interrupt handler for keyboard */
void init_keyboard(void);


/* Procedure keyboard_int:
 * This method is what is called by the actual hardware interrupt. Calls 
 * keyboard_int and acknowledges the interrupt
 */
void keyboard_handler();

/* Procedure keyboard_getc:
 * void -> int
 * Read a character from the keyboard
 * Pre: nil
 * Post: nil
 * Return: a character
 */
uint8_t keyboard_getc(void);

/* Map of shift modifier keys */
static unsigned char shiftcode[256] = {
  [0x2A] SHIFT,
  [0x36] SHIFT
};

/* map of 'normal' (unmodified, non-modifier) keys */
/* Modifier keys are assigned NOK */
static unsigned char normalmap[256] = {
  NOK, ESC, '1', '2', '3', '4', '5', '6', /* 0x00 */ 
  '7', '8', '9', '0', '-', '=', BSP, TAB, 
  'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* 0x10 */ 
  'o', 'p', '[', ']', NWL, NOK, 'a', 's', 
  'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 0x20 */ 
  QUO, '`', NOK, BSL, 'z', 'x', 'c', 'v', 
  'b', 'n', 'm', ',', '.', '/', NOK, '*', /* 0x30 */ 
  NOK, ' ', NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, '7', /* 0x40 */ 
  '8', '9', '-', '4', '5', '6', '+', '1',
  '2', '3', '0', '.', NOK, NOK, NOK, NOK, /* 0x50 */ 
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK, /* 0x60 */ 
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK, /* 0x70 */ 
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK, /* 0x80 */
};

/* map of keys when SHIFT is DOWN */
static unsigned char shiftmap[256] = {
  NOK, ESC, '!', '@', '#', '$', '%', '^', /* 0x00 */ 
  '&', '*', '(', ')', '-', '=', BSP, TAB, 
  'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* 0x10 */ 
  'O', 'P', '{', '}', NWL, NOK, 'A', 'S', 
  'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 0x20 */ 
  '"', '~', NOK, '|', 'Z', 'X', 'C', 'V',
  'B', 'N', 'M', '<', '>', '?', NOK, '*', /* 0x30 */ 
  NOK, ' ', NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, '7', /* 0x40 */ 
  '8', '9', '-', '4', '5', '6', '+', '1',
  '2', '3', '0', '.', NOK, NOK, NOK, NOK, /* 0x50 */ 
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK, /* 0x60 */ 
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK, /* 0x70 */ 
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK,
  NOK, NOK, NOK, NOK, NOK, NOK, NOK, NOK, /* 0x80 */
};

#endif /* _XXXOS_KEYBOARD_H */
